package com.xrui.hbaseview.ui.design;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import java.util.Arrays;
import java.util.List;

public class JTableModel extends DefaultTableModel {

    private static final long serialVersionUID = 7602909046413522975L;
    private List<Integer> editableColumns;

    public JTableModel(Integer... editableColumns) {
        this.editableColumns = Arrays.asList(editableColumns);
    }

    /**
     * Stops editing of the cell if there is any.
     *
     * @param table The table that contains the cell.
     */
    public static void stopCellEditing(JTable table) {
        if (table.getRowCount() > 0) {
            TableCellEditor editor = table.getCellEditor();
            if (editor != null) {
                editor.stopCellEditing();
            }
        }
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return editableColumns.contains(column);
    }
}
